package com.action.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import com.service.interfaces.UserService;
import com.utils.Constants;
import com.utils.CookieTool;
import com.entity.User;

@Scope("prototype")
public class UserAction extends BaseAction{
	private User user;
	
	@Resource(name = "userService")
	private UserService userService;
	
	public String login() {
		if(user != null){
			User user2 = userService.login(user);
			if(user2 != null){
				ServletActionContext.getRequest().getSession().setAttribute("user", user);
				HttpServletResponse response = ServletActionContext.getResponse();
				CookieTool.addCookie(response, "username", user.getUsername(), 600);
				CookieTool.addCookie(response, "password", user.getPassword(), 600);
				
				return getReturn();
			}
		}else{
			addFieldError("validateError", "login failed.");
			return getReturn();
		}
		addFieldError("validateError", "login failed.");
		
		return getReturn();
	}
	
	public String logout() {
		ServletActionContext.getRequest().getSession().removeAttribute("user");
//		CookieTool.deleteCookie(request, response, "username");
//		CookieTool.deleteCookie(request, response, "password");
		return getReturn();
	}
	
	public String test() {
		userService.test();
		
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	
}



















